package hnqigczy.zzptc.com.weather;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import hnqigczy.zzptc.com.weather.entity.CityInfo;
import hnqigczy.zzptc.com.weather.entity.DayWeather;
import hnqigczy.zzptc.com.weather.entity.Weather;
import hnqigczy.zzptc.com.weather.entity.WertherData;

public class MainActivity extends AppCompatActivity {
    private String str;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Toast.makeText(MainActivity.this, msg.obj.toString(), Toast.LENGTH_SHORT).show();
            str = msg.obj.toString();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void getList(View view) {
        //解析str
        try {
            JSONObject jsonobject=new JSONObject(str);
            JSONObject jsonobject1= jsonobject.getJSONObject("cityInfo");
            String cityname= jsonobject1.getString("city");
            String cityId= jsonobject1.getString("cityId");
            String parent= jsonobject1.getString("parent");
            String updateTime= jsonobject1.getString("updateTime");
            //构造Cityinfo对象
            CityInfo cityinfo=new CityInfo();
            cityinfo.setCity(cityname);
            cityinfo.setCityId(cityId);
            cityinfo.setParent(parent);
            cityinfo.setUpdateTime(updateTime);

            JSONObject jsonobject2=  jsonobject.getJSONObject("data");
            JSONArray jsonarray2=jsonobject2.getJSONArray("forecast");
            JSONObject jsonobjectyesterday=jsonobject2.getJSONObject("yesterday");
           String ganmao= (String) jsonobject2.get("ganmao");
           Double pm10= (Double) jsonobject2.get("pm10");
            Double pm25= (Double) jsonobject2.get("pm25");
           String quality= (String) jsonobject2.get("quality");
           String shidu= (String) jsonobject2.get("shidu");
           String wendu= (String) jsonobject2.get("wendu");
            List<DayWeather> list=new ArrayList<DayWeather>();
            for(int i=0;i<jsonarray2.length();i++){
                 JSONObject jsono= (JSONObject) jsonarray2.get(i);
                Double api= (Double) jsono.get("aqi");
                String date=jsono.getString("date");
                String fl=jsono.getString("fl");
                String fx=jsono.getString("fx");
                String high=jsono.getString("high");
                String low=jsono.getString("low");
                String notice=jsono.getString("notice");
                String sunrise=jsono.getString("sunrise");
                String sunset=jsono.getString("sunset");
                String type=jsono.getString("type");
                DayWeather day=new DayWeather();
                day.setApi(api);
               day.setDatetime(date);
                day.setFl(fl);
                day.setFx(fx);
                day.setHigh(high);
                day.setLow(low);
                day.setNotice(notice);
                day.setSunrice(sunrise);
                day.setSunset(sunset);
                day.setType(type);
                list.add(day);
            }

          DayWeather yesterday=new DayWeather();
            yesterday.setApi((Double) jsonobjectyesterday.get("aqi"));
            yesterday.setFl(jsonobjectyesterday.getString("fl"));
            yesterday.setFx(jsonobjectyesterday.getString("fx"));
            yesterday.setHigh(jsonobjectyesterday.getString("high"));
            yesterday.setLow(jsonobjectyesterday.getString("low"));
            yesterday.setNotice(jsonobjectyesterday.getString("notice"));
            yesterday.setSunrice(jsonobjectyesterday.getString("sunrise"));
            yesterday.setSunset(jsonobjectyesterday.getString("sunset"));
            yesterday.setType(jsonobjectyesterday.getString("type"));

          String date=  jsonobject.getString("date");
          String message=  jsonobject.getString("message");
          String status=  jsonobject.getString("status");
          String time=  jsonobject.getString("time");


          WertherData wertherdata=new WertherData();
            wertherdata.setGanmao(ganmao);
            wertherdata.setForecast(list);
            wertherdata.setPm10(pm10);
            wertherdata.setPm25(pm25);
            wertherdata.setQuality(quality);
            wertherdata.setShidu(shidu);
            wertherdata.setWendu(wendu);
            wertherdata.setYesterday(yesterday);

            Weather weather=new Weather();
            weather.setCityInfo(cityinfo);
            weather.setData(wertherdata);
            weather.setDatatime(date);
            weather.setMessage(message);
            weather.setStatus(status);
            weather.setTime(time);

            Toast.makeText(this, weather.getData().getForecast().get(0).getFl(), Toast.LENGTH_SHORT).show();





        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

    public void getData(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL("http://t.weather.sojson.com/api/weather/city/101030100");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    if (conn.getResponseCode() == 200) {
                        InputStream in = conn.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                        String s = reader.readLine();
                        //  Bitmap bitmap1= BitmapFactory.decodeStream(in);
                        //异步大师要我们按规范做事
                        Message message = handler.obtainMessage();
                        message.obj = s;
                        handler.sendMessage(message);

                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }).start();


    }
}
